<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Cases extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'cases';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state_text',
        'he_time_text'
    ];
    

    
    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3'), '4' => __('State 4'), '5' => __('State 5'), '8' => __('State 8'), '11' => __('State 11'), '12' => __('State 12'), '13' => __('State 13'), '14' => __('State 14')];
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['he_time']) ? $data['he_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setHeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function affair()
    {
        return $this->belongsTo('Affair', 'affair_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shiming()
    {
        return $this->belongsTo('Shiming', 'user_id', 'user_id', [], 'LEFT')->setEagerlyType(0);
    }
}
